package com.fu.utils;

import com.fu.response.RestResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @Class : RestResultUtils
* @description : RestResult生成类
* @author : Little Ant
* @creation Date : 2017/11/1
* @ModificationHistory   who      when        what
* -------  -----------  ------------------------------
*/
public class RestResultUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(RestResultUtils.class);

    /**
     *
     * @param code
     * @param data
     * @param message
     * @param <T>
     * @return
     */
    private static<T> RestResult<T> genResult(String status,String code,T data, String message){
        RestResult restResult = RestResult.newInstance();
        restResult.setStatus(status);
        restResult.setData(data);
        restResult.setMessage(message);
        restResult.setCode(code);
        LOGGER.debug("generate rest result:{}",restResult);
        return restResult;
    }

    /**
    * @param : data
    * @description : 请求成功应答(自定义业务数据)
    * @return
    */
    public static<T> RestResult<T> genSuccessResult(T data){
        return genResult(Constants.SUCCESS_MESSAGE,Constants.SUCCESS_CODE,data,Constants.SUCCESS_MESSAGE);
    }

    /**
    * @param :
    * @description : 请求成功应答(不带业务数据)
    * @return
    */
    public static<T> RestResult<T> genSuccessResult(){
        return genResult(Constants.SUCCESS_MESSAGE,Constants.SUCCESS_CODE,null,Constants.SUCCESS_MESSAGE);
    }

    /**
    * @param : data, message
    * @description : 请求成功应答(自定义业务数据及消息提示)
    * @return
    */
    public static<T> RestResult<T> genSuccessResult(T data,String message){
        return genResult(Constants.SUCCESS_MESSAGE,Constants.SUCCESS_CODE,data, message);
    }

    /**
    * @param : errorCode, message
    * @description : 请求错误应答(自定义错误码及错误描述)
    * @return
    */
    public static<T> RestResult<T> genErrorResult(String errorCode,String message){
        return genResult(Constants.ERROR_MESSAGE,errorCode,null,message);
    }

}
